<?php
namespace app\common\controller;

use app\common\model\SystemLog;
use org\Auth;
use think\Loader;
use think\Cache;
use think\Controller;
use think\Db;
use think\Session;

/**
 * 后台公用基础控制器
 * Class AdminBase
 * @package app\common\controller
 */
class AdminBase extends Controller
{
    protected function _initialize()
    {
        parent::_initialize();

        $this->checkAuth();
        $this->getMenu();

        // 输出当前请求控制器（配合后台侧边菜单选中状态）
        $this->assign('controller', Loader::parseName($this->request->controller()));
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {

        if (!Session::has('admin_id')) {
            $this->redirect('admin/login/index');
        }

        $module     = $this->request->module();
        $controller = $this->request->controller();
        $action     = $this->request->action();

        // 排除权限
        $not_check = ['admin/Index/index', 'admin/AuthGroup/getjson', 'admin/System/clear'];

        if (!in_array($module . '/' . $controller . '/' . $action, $not_check)) {
            $auth     = new Auth();
            $admin_id = Session::get('admin_id');
//            if (!$auth->check($module . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 1) {
//                $this->error('没有权限');
//            }
        }
    }

    /**
     * 获取侧边栏菜单
     */
    protected function getMenu()
    {
        $menu     = [];
        $admin_id = Session::get('admin_id');
        $auth     = new Auth();

        $auth_rule_list = Db::name('auth_rule')->where('status', 1)->order(['sort' => 'DESC', 'id' => 'ASC'])->select();
        foreach ($auth_rule_list as $value) {
            if ($auth->check($value['name'], $admin_id) || $admin_id == 1) {
                $menu[] = $value;
            }
        }
        $menu = !empty($menu) ? array2tree($menu) : [];
        $this->assign('menu', $menu);
    }

    /**
     * @param $i 1添加 2删除 3修改 4禁用 5审核
     * @param $title
     */
    public function getLog($i, $title)
    {
        $username = Session::get('admin_id');
        //先创建一个log表，有id, action, username, query, time 等字段，如果需要记录更多，可添加
        $query_string = $_SERVER['QUERY_STRING'];//查询(query)的字符串,这个最好处理一下

        $ip = $this->request->ip(0,true);
//$username就是当前操作人的名字了，登录后都有的吧
        if ($i == 1) {
            $this->addlog("添加", $title, $username, $query_string,$ip);
        }
        if ($i == 2) {
            $this->addlog("删除", $title, $username, $query_string,$ip);
        }
        if ($i == 3) {
            $this->addlog("修改", $title, $username, $query_string,$ip);
        }
        if ($i == 4) {
            $this->addlog("禁用", $title, $username, $query_string,$ip);
        }
        if ($i == 5) {
            $this->addlog("审核", $title, $username, $query_string,$ip);
        }

    }

    function addlog($action, $title, $username, $query_string,$ip)
    {
        //这里可以把时间和$query_string处理一下，插入数据库

        $operation = $action . "" . $title;
        SystemLog::create([
            'admin_user_id'=>$username,
            'operation'=>$operation,
            'query_string'=>$query_string,
            'create_time'=>date("Y-m-d H-i-s"),
            'user_ip'=>$ip,
        ]);

    }

}